package collection_;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author 小闽
 * @Date 2024/10/21 22:50
 * @PackageName: collection_
 * @ClassName: CollectionExercise
 * @Description: 练习
 * @Version 1.0
 */
public class CollectionExercise {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        /**
         * 1. 创建3个Dog {name,age} 对象，放入到 ArrayList中，赋给List引用
         * 2. 用迭代器和增强for循环两种方式来遍历
         * 3. 重写 Dog 的 toString 方法，输出 name 和 age
         */
        List list = new ArrayList();
        list.add(new Dog("小红",21));
        list.add(new Dog("小白",22));
        list.add(new Dog("小蓝",23));

        // 使用迭代器来进行遍历
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("next=" + next);
        }
        System.out.println("===========================");
        // 使用增强型for循环来遍历
        for (Object object : list) {
            System.out.println("object=" + object);
        }

    }
}

class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}